<?php
/**
 * Special price list admin filter
 *
 * @author Lukas Normantas
 */
class Catalog_Model_ProductAdminFilter extends Areal_Model_CollectionFilter
{
    public function filterQuery(Doctrine_Query $query)
    {
        $query->orderBy($this->getSort() );
        return $query;
    }

    public function getDefaultSort()
    {
        return 'p.created_at';
    }

    public function getDefaultData()
    {
        return array( 'phrase' => '');
    }

    /*
     * Fetches paginator from active products with active specials of $this category
     */
    public function getProductAdminPaginator($category_id, $current_page)
    {
        $q = Doctrine_Query::create()
           ->select('c.*, p.*, s.*')
           ->from( 'Catalog_Model_Product p' )
           ->leftJoin( 'p.Category c' )
           ->leftJoin( 'p.Specials s ON s.product_id = p.id '
                       .'AND s.start_at <= CURRENT_DATE AND s.end_at>=CURRENT_DATE')
           ->where('c.id = ?', $category_id )
           ->orderBy($this->getSort());
        $this->filterQuery($q);
        $results_per_page = 10;
        $doctrinePager = new Doctrine_Pager($q, $current_page, $results_per_page);
        $zendPaginator = Areal_Paginator::build($doctrinePager);
        return $zendPaginator;
    }



}